<?
	class Connection
	{
		private $connection;
		private $dbName;
		
		function __construct($host = null, $user = null, $pswd = null)
		{
			if(empty($host)){
				$host = App::get('db_host');
				$user = App::get('db_login');
				$pswd = App::get('db_password');
			}
		
			$this->connection = mysql_connect($host, $user, $pswd) 
				or die ('Sql Error: ' . mysql_error());
				
			$this->query("SET NAMES 'utf8'");
			$this->query("SET CHARACTER SET 'utf8'");
			$this->query("SET SESSION collation_connection = 'utf8_general_ci'");
		}
		
		public function query($sql, &$result = null)
		{
			if(empty($this->dbName)){
				$this->db(App::get('db_name'));
			}
			
            $result = mysql_query($sql, $this->connection)
                or die ('Sql Error: ' . mysql_error());

			return $this;
		}
		
		public function db($name)
		{
			$this->dbName = $name;
			
			mysql_select_db($name, $this->connection);
			
			return $this;
		}
		
		public function close()
		{
			mysql_close($this->connection)
                or die ('Sql Error: ' . mysql_error());
		}
	}
?>